#include "Application.hpp"

Application::Application(QMainWindow* /*parent*/, Qt::WFlags /*flags*/) :
    ui(new Ui_MainWindow()), cm(new ConfigManager()), lw(new CollectionWidget()), ww(new WishlistWidget()), hw(new HistoryWidget()) {
    player=new Player();
    //pl=player;
    ui->setupUi(this);
    setupUi();
    setupPlayer();
}
Application::~Application() {
    player->stop();
    disconnect(ui->tbPlay, SIGNAL(clicked()), player, SLOT(playOrPause()));
    disconnect(ui->tbStop, SIGNAL(clicked()), player, SLOT(stop()));
    disconnect(ui->actionPlay, SIGNAL(triggered()), player, SLOT(playOrPause()));
    //disconnect(ui->actionStop, SIGNAL(triggered()), player, SLOT(stop()));
    disconnect(ui->sliderVolume, SIGNAL(sliderMoved(int)), player, SLOT(setVolume(int)));
    delete lw;
    delete hw; delete ww;
    delete player;
    delete ui;
    delete cm;
}

void Application::setupPlayer() {
    //set popup menus
    //set signals
    connect(ui->tbPlay, SIGNAL(clicked()), player, SLOT(playOrPause()));
    connect(ui->tbStop, SIGNAL(clicked()), player, SLOT(stop()));
    connect(ui->actionPlay, SIGNAL(triggered()), player, SLOT(playOrPause()));
    //connect(ui->actionStop, SIGNAL(triggered()), player, SLOT(stop()));
    connect(ui->sliderVolume, SIGNAL(valueChanged(int)), player, SLOT(setVolume(int)));
}
void Application::setupUi() {
    ui->leftFrame->addWidget(lw);
    ui->leftFrame->addWidget(hw);
    ui->leftFrame->addWidget(ww);
    connect(ui->tbCollection, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    connect(ui->tbFiles, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    connect(ui->tbFavorites, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    connect(ui->tbWishlist, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    connect(ui->tbPlaylists, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    connect(ui->tbHistory, SIGNAL(clicked()), this, SLOT(toggleLeftPanel()));
    ui->leftFrame->setCurrentIndex(0);
}

void Application::updateSeeking(int ms, int duration) {
    ui->sliderSeek->setValue((int)100*ms/duration);
    /*int ss=ms/1000;
    int h=ss/3600, m, s;
    m=(ss/60)%60;
    s=ss%60;*/
    ui->labelElapsed->setText("");
    ui->labelRemaining->setText("");
}

void Application::setToggleMute() {

}

void Application::toggleLeftPanel() {
    ui->tbCollection->setChecked(false);
    ui->tbFiles->setChecked(false);
    ui->tbFavorites->setChecked(false);
    ui->tbWishlist->setChecked(false);
    ui->tbPlaylists->setChecked(false);
    ui->tbHistory->setChecked(false);
    if(sender()==ui->tbCollection) {
        ui->tbCollection->setChecked(true);
        ui->leftFrame->setCurrentWidget(lw);
    } else if(sender()==ui->tbFiles) {
        ui->tbFiles->setChecked(true);
    } else if(sender()==ui->tbFavorites) {
        ui->tbFavorites->setChecked(true);
    } else if(sender()==ui->tbWishlist) {
        ui->tbWishlist->setChecked(true);
        ui->leftFrame->setCurrentWidget(ww);
    } else if(sender()==ui->tbPlaylists) {
        ui->tbPlaylists->setChecked(true);
    } else if(sender()==ui->tbHistory) {
        ui->tbHistory->setChecked(true);
        ui->leftFrame->setCurrentWidget(hw);
    }
}

int main(int argc, char** argv) {
    QtSingleApplication sa(argc, argv);
    if(sa.isRunning()) return 0; //allready running
    Application a;
    a.show();
    return sa.exec();
}
